/*
 * @Author: szx
 * @Date: 2022-03-27 13:13:21
 * @LastEditTime: 2022-03-27 13:16:56
 * @Description:
 * @FilePath: \leetcode\剑指offer\54\54.js
 */
function TreeNode(val) {
    this.val = val;
    this.left = this.right = null;
}

/**
 * @param {TreeNode} root
 * @param {number} k
 * @return {number}
 */
var kthLargest = function (root, k) {
    if (!root) return 0;
    let res = 0;
    const dfs = (root, k) => {
        root.right && dfs(root.right, k--);
        if (k == 0) {
            res = root.val;
            return;
        }
        root.left && dfs(root.left, k--);
    };
    dfs(root, k);
    return res;
};
